package main
import (
    "fmt"
	"time"
	"math/rand"
	"os"
	"os/exec"
)
func main () {
    //JSRUN引擎2.0，支持多达30种语言在线运行，全仿真在线交互输入输出。
	cmd := exec.Command("cmd","/c","cls")
	cmd.Stdout = os.Stdout
	cmd.Run()

	
	if_test()

    fmt.Println("Hello JSRUN!   \n\n         - from Golang .")
}



// -----------------------
/*
	属性变更
	sp := WithName("1121")
	so := ServOption{
		name:"222",
	}
	sp(&so)
    fmt.Println(so.name)
*/
type ServOption struct {
	Id 		int64
	name 	string
	Message	string
}

type ServOptions func(o *ServOption)

func WithName(name string) ServOptions {
	return func(o *ServOption){
		o.name = name
	}
}

// ---------------------------

func if_test()  {

	values := make(map[int]bool)
    values[1] = if_1()
    values[2] = if_2()
    values[3] = if_3()

    if values[1] {
         fmt.Println("if_1 true")
    }else{
        if values[2] {
            fmt.Println("if_2 true")
        }else{
            if values[3] {
                fmt.Println("if_3 true")
            }else{
                fmt.Println("if_3 false")
            }
            fmt.Println("if_2 false")
        }
        fmt.Println("if_1 false")
    }

    fmt.Println(" --------- ")
	
	/*

    c := make(chan int)
	ol := make(chan bool)
	go func ()  {
		<- ol
		close(ol)
	}()
	
	go func ()  {
		for b := range values {
			fmt.Println("c <- ",b)
			c <- b
		}
		ol <- true
    }()
	*/
	fmt.Println(" --------- if_v1 ")
	// ok,msg := if_1_v1()
	// if !ok {
	// 	ok,msg = if_2_v1()
	// 	fmt.Println(msg)
	// }
 	/*
	arr := []int{1,2,3,4,5}
	for _,v := range arr {
		
		fmt.Println(arr)
		fmt.Println(v)
		arr = arr[1:]
	}*/
	runFuncIF( []func(num int)(bool,string){ if_1_v1 , if_2_v1, if_3_v1 },true)
	
}

// 多成嵌套判断执行
// runFuncIF( []func(num int)(bool,string){ if_1_v1 , if_2_v1, if_3_v1 },true)
func runFuncIF(ff []func(num int)(bool,string),r bool) {
	fmt.Println(ff)
	rd := rand.New(rand.NewSource(time.Now().UnixNano()))
	num := getInt(rd)
	for _,f := range ff {
		if !r {
			return
		}
		ok,msg := f(num)
		if !ok {
			fmt.Println(msg)
			ff = ff[1:]
			runFuncIF(ff,false)
		}else{
			fmt.Println(">>>>> 成功退出 ",f)
			return
		}
	}
}

// 随机返回
func if_1_v1(num int) (bool,string){
	b :=  num % 2 == 1
	msg := ""
	if !b {
		msg = "if_1 false"
	}

	fmt.Println(" --------- f1 ",msg)
	return b,msg
}

func if_2_v1(num int) (bool,string){
    b :=  num % 2 == 1
	msg := ""
	if !b {
		msg = "if_2 false"
	}
	fmt.Println(" --------- f2 ",msg)
	return b,msg
}

func if_3_v1(num int) (bool,string){
	b :=  num % 2 == 1
	msg := ""
	if !b {
		msg = "if_3 false"
	}
	fmt.Println(" --------- f3 ",msg)
	return b,msg
}

func getInt(r *rand.Rand) int {
	d := r.Intn(100)
	fmt.Println(" --------- d ",d)
	return	d
}


func if_1() bool {
    rand.Seed(time.Now().UnixNano())
    return  rand.Intn(10) % 2 == 1
}

func if_2() bool {
    rand.Seed(time.Now().UnixNano())
    return  rand.Intn(10) % 2 == 1
}


func if_3() bool {
    rand.Seed(time.Now().UnixNano())
    return  rand.Intn(10) % 2 == 1
}